﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SQLite;
using System.Data;

namespace PTCScheduler
{
    /// <summary>
    /// Interaction logic for ViewBy_StudentPage.xaml
    /// </summary>
    public partial class ViewBy_StudentPage : Page, ScheduleReport
    {
        //SQlite connection for student page
        private SQLiteConnection conn;

        //SQlite data adapter for student page
        private SQLiteDataAdapter da;

        //SQLite connection string
        private String connString = SystemQueries.dbConnString();

        //Empty Constructor
        public ViewBy_StudentPage()
        {
            InitializeComponent();
        }

        //Connect to the database
        public void dbConn()
        {
            //Create connection.
            conn = new SQLiteConnection(connString);

            //Open connection
            conn.Open();
        }

        //Implement the results for the query to fill datagridview.
        public void queryResults()
        {
            try
            {
                //Retrieve the student query string from the SystemQueries class and set the
                //student id to the desired student ID in studentIDtextbox.
                String sql = SystemQueries.studentQueryString(Convert.ToInt32(studentIDtextbox.Text));

                //Create dataset to fill with the results of the query. 
                DataSet ds = new DataSet(); //System.Data assembly

                //User SQLiteDataAdapter to retrieve results. 
                da = new SQLiteDataAdapter(sql, connString);

                //Fill the dataset with the results. 
                da.Fill(ds);

                //If there are no records then prompt user that there are no scheduled
                //conferences for student. Otherwise just display records. 
                if (ds.Tables[0].Rows.Count == 0)
                {
                    nostudentconferenceslbl.Visibility = Visibility.Visible;
                }

                //Format date and time column
                dtFormat(ds);

                //Display results in datagrid view. 
                dataGridStudents.ItemsSource = ds.Tables[0].DefaultView;
            }
            catch (Exception)
            {
                    //Display the warning label to prompt user to enter valid number.
                    studentwarninglbl.Visibility = Visibility.Visible;
            }
            finally
            {
                //Close connection and dispose.
                conn.Close();
                conn.Dispose();
            }
        }

        //Method to format the date and time column
        private void dtFormat(DataSet ds)
        {
            //Iterate through each row and format accordinly
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                //Create date time object
                DateTime dt = DateTime.Parse(row["Time"].ToString());
                //Set column equal to desired date pattern.
                row["Time"] = dt.ToString("hh:mm tt");
            }
        }

        //Search button event is clicked. Query the results and display in datagrid.
        private void searchbystudentbtn_Click(object sender, RoutedEventArgs e)
        {
            //Hide all labels.
            HideAllLabels();

            //Open connection
            dbConn();

            //Query the results and fill datagrid.
            queryResults();

            //Clear the form.
            ClearForm();
        }

        //Method to hide all labels
        private void HideAllLabels()
        {
            //Hide labels.
            studentwarninglbl.Visibility = Visibility.Hidden;
            nostudentconferenceslbl.Visibility = Visibility.Hidden;
        }

        //Clear Form
        private void ClearForm()
        {
            //Clear textbox.
            studentIDtextbox.Clear();

            //Refocus to textbox.
            studentIDtextbox.Focus();
        }

        //Handles Export Button Click Event.
        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            //Check to ensure datagrid has data to export.
            if (dataGridStudents.Items.Count > 0)
            {
                //Create new instance of CSVExport object which invokes the export to CSV method
                //to export data to excel worksheet.
                new CSVExport(dataGridStudents);
            }
        }

        //Handles Print Button Click Event.
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            //Create new instance of the PrintDataGrid object which invokes the print feature
            //that prints contents of datagrid.
            new PrintDataGrid(dataGridStudents);
        }

        
    }
}
